package com.yixun.qfbao.job;

import com.yixun.qfbao.api.service.SendApiService;
import com.yixun.qfbao.apidto.SendIntentionListResDto;
import com.yixun.qfbao.apidto.SendPremisesListResDto;
import com.yixun.qfbao.apidto.SendReqDto;
import com.yixun.qfbao.apidto.SendVisitListResDto;
import com.yixun.qfbao.mapper.ApiLogMapper;
import com.yixun.qfbao.util.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;


/**
 * 恒信 定时推送
 * 楼盘信息、到访数据、报备信息
 *
 * @author liuqi
 */
@Component
public class SendDataJob {
    private static Logger logger = LoggerFactory.getLogger(SendDataJob.class);

    private static final String PREMISES_URL = "http://122.4.193.238:5556/putProjectData";
    private static final String VISIT_URL = "http://122.4.193.238:5556/putVisitData";
    private static final String INTENTION_URL = "http://122.4.193.238:5556/putRecordData";

    private static final String PREMISES_TIME = "LAST_SEND_PREMISES_TIME";
    private static final String VISIT_TIME = "LAST_SEND_VISIT_TIME";
    private static final String INTENTION_TIME = "LAST_SEND_INTENTION_TIME";

    private static final Long COMPANY_ID = 16020L;

    private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private SendApiService sendApiService;
    @Autowired
    private ApiLogMapper apiLogMapper;

//    @Scheduled(cron = "0 0 0/2 * * ?")
    public void sendPremises() {
        logger.info("---------------->触发定时推送楼盘信息任务！");
        SendReqDto dto = new SendReqDto();
        /**
         * 推送楼盘信息
         */
        if (RedisUtil.hasKey(PREMISES_TIME)) {
            /**
             * 通过缓存获取上次推送时间
             */
            String lastTime = RedisUtil.get(PREMISES_TIME).toString();
            logger.info("redis缓存的上次楼盘信息推送时间{}", lastTime);
            if (null != lastTime) {
                dto.setCompanyId(COMPANY_ID);
                dto.setLastTime(lastTime);
                //触发推送接口
                SendPremisesListResDto resDto = sendApiService.sendPremisesList(dto);
                //缓存本次推送时间
                RedisUtil.set(PREMISES_TIME, df.format(Calendar.getInstance().getTime()));
                logger.info("定时楼盘信息推送->结果：{}", resDto);
                return;
            }
        }
        /**
         * 通过 tm_api_log 获取上次推送 时间
         */
        List<String> timeList = apiLogMapper.listApiLogTime(PREMISES_URL, COMPANY_ID);
        if (CollectionUtils.isNotEmpty(timeList)) {
            String lastTime = timeList.get(0);
            logger.info("tm_api_log 储存的上次楼盘信息推送时间{}", lastTime);
            dto.setCompanyId(COMPANY_ID);
            dto.setLastTime(lastTime);
            //触发推送接口
            SendPremisesListResDto resDto = sendApiService.sendPremisesList(dto);
            logger.info("定时楼盘信息推送->结果：{}", resDto);
        }
        //缓存本次推送时间
        RedisUtil.set(PREMISES_TIME, df.format(Calendar.getInstance().getTime()));
    }

//    @Scheduled(cron = "0 0 0/2 * * ?")
    public void sendVisit() {
        logger.info("---------------->触发定时推送到访数据任务！");
        SendReqDto dto = new SendReqDto();
        /**
         * 推送到访数据
         */
        if (RedisUtil.hasKey(VISIT_TIME)) {
            /**
             * 通过缓存获取上次推送时间
             */
            String lastTime = RedisUtil.get(VISIT_TIME).toString();
            logger.info("redis缓存的上次到访数据送时间{}", lastTime);
            if (null != lastTime) {
                dto.setCompanyId(COMPANY_ID);
                dto.setLastTime(lastTime);
                //触发推送接口
                SendVisitListResDto resDto = sendApiService.sendVisitList(dto);
                //缓存本次推送时间
                RedisUtil.set(VISIT_TIME, df.format(Calendar.getInstance().getTime()));
                logger.info("定时到访数据推送->结果：{}", resDto);
                return;
            }
        }
        /**
         * 通过 tm_api_log 获取上次推送 时间
         */
        List<String> timeList = apiLogMapper.listApiLogTime(VISIT_URL, COMPANY_ID);
        if (CollectionUtils.isNotEmpty(timeList)) {
            String lastTime = timeList.get(0);
            logger.info("tm_api_log 储存的上次到访数据推送时间{}", lastTime);
            dto.setCompanyId(COMPANY_ID);
            dto.setLastTime(lastTime);
            //触发推送接口
            SendVisitListResDto resDto = sendApiService.sendVisitList(dto);
            logger.info("定时到访数据推送->结果：{}", resDto);
        }
        //缓存本次推送时间
        RedisUtil.set(VISIT_TIME, df.format(Calendar.getInstance().getTime()));
    }

//    @Scheduled(cron = "0 0 0/2 * * ?")
    public void sendIntention() {
        logger.info("---------------->触发定时推送报备信息任务！");
        SendReqDto dto = new SendReqDto();
        /**
         * 推送报备信息
         */
        if (RedisUtil.hasKey(INTENTION_TIME)) {
            /**
             * 通过缓存获取上次推送时间
             */
            String lastTime = RedisUtil.get(INTENTION_TIME).toString();
            logger.info("redis缓存的上次报备信息推送时间{}", lastTime);
            if (null != lastTime) {
                dto.setCompanyId(COMPANY_ID);
                dto.setLastTime(lastTime);
                //触发推送接口
                SendIntentionListResDto resDto = sendApiService.sendIntentionList(dto);
                //缓存本次推送时间
                RedisUtil.set(INTENTION_TIME, df.format(Calendar.getInstance().getTime()));
                logger.info("定时报备信息推送->结果：{}", resDto);
                return;
            }
        }
        /**
         * 通过 tm_api_log 获取上次推送 时间
         */
        List<String> timeList = apiLogMapper.listApiLogTime(INTENTION_URL, COMPANY_ID);
        if (CollectionUtils.isNotEmpty(timeList)) {
            String lastTime = timeList.get(0);
            logger.info("tm_api_log 储存的上次报备信息推送时间{}", lastTime);
            dto.setCompanyId(COMPANY_ID);
            dto.setLastTime(lastTime);
            //触发推送接口
            SendIntentionListResDto resDto = sendApiService.sendIntentionList(dto);
            logger.info("定时报备信息推送->结果：{}", resDto);
        }
        //缓存本次推送时间
        RedisUtil.set(INTENTION_TIME, df.format(Calendar.getInstance().getTime()));
    }

}
